﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Modelo.Modulos.ClasesModelado;

namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
   public class ComentarioBD
   {
       public void InsertarComentario(SqlCommand comando, ComentarioMod obj)
       {
           try
           {
               string sp = "spInsertarComentarios";

               comando.CommandType = CommandType.StoredProcedure;
               comando.CommandText = sp;
               comando.Parameters.Clear();


               comando.Parameters.Add(new SqlParameter("@IdTarea", SqlDbType.Int));
               comando.Parameters.Add(new SqlParameter("@Comentario", SqlDbType.VarChar));
               comando.Parameters.Add(new SqlParameter("@FechaComentario", SqlDbType.DateTime));

               comando.Parameters[0].Value = obj.IdTarea;
               comando.Parameters[1].Value = obj.Comentario;
               comando.Parameters[2].Value = obj.FechaComentario;
              

               comando.ExecuteNonQuery();


           }
           catch (Exception exc)
           {
               throw exc;
           }
       }

       public List<ComentarioMod> ConsultarCometarioById(SqlCommand comando, ComentarioMod objs)
       {
           try
           {
               string sp = "spConsultarComentariosbyIs";

               comando.CommandType = CommandType.StoredProcedure;
               comando.CommandText = sp;
               comando.Parameters.Clear();

               comando.Parameters.Add(new SqlParameter("@IdTarea", SqlDbType.Int));

               comando.Parameters[0].Value = objs.IdTarea;

               IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
               DataSet ds = new DataSet();
               da.Fill(ds);

               DataTable dt = ds.Tables[0];
               List<ComentarioMod> lista = new List<ComentarioMod>();

               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   ComentarioMod obj = new ComentarioMod();

                   obj.IdTarea = Convert.ToInt32(dt.Rows[i]["IdTarea"]);
                   obj.IdComentario = Convert.ToInt32(dt.Rows[i]["IdComentario"]);
                   obj.Comentario = dt.Rows[i]["Comentario"].ToString();
                   obj.FechaComentario = Convert.ToDateTime(dt.Rows[i]["FechaComentario"]);
                   lista.Add(obj);
               }


               return lista;

           }
           catch (Exception exc)
           {
               throw exc;
           }
       }

       public void EliminarComentario(SqlCommand comando, ComentarioMod obj)
       {
           try
           {
               string sp = "spEliminaComentario";

               comando.CommandType = CommandType.StoredProcedure;
               comando.CommandText = sp;
               comando.Parameters.Clear();


               comando.Parameters.Add(new SqlParameter("@IdComentario", SqlDbType.Int));

               comando.Parameters[0].Value = obj.IdComentario;

               comando.ExecuteNonQuery();


           }
           catch (Exception exc)
           {
               throw exc;
           }
       }

    }
}
